<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/users
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$this->load->model('Users_model', 'user');
      	if( !$this->user->loggedin() )	redirect('/login', 'refresh');
		
		$is_admin = $this->user->is_admin();
		$data = array('navigation' => 0, 'is_admin' => $is_admin);
		$navigation = $this->load->view('default/user/navigation', $data, true);
		
		$data = array('title' => 'FAT Application | Log in');
		$this->load->view('default/header', $data);
		
		
		$userData = $this->user->get();
		
		$associationsData = $this->user->get_associations();
		$data = array('userData' => $userData, 'associationsData' => $associationsData, 'navigation' => $navigation);
		$this->load->view('default/user/profile', $data);
		
		$this->load->view('default/footer');
	}	
	
	public function associations()
	{
		$this->load->model('Users_model', 'user');
      	if( !$this->user->loggedin() )	redirect('/login', 'refresh');

		$is_admin = $this->user->is_admin();
		$data = array('navigation' => 10, 'is_admin' => $is_admin);
		$navigation = $this->load->view('default/user/navigation', $data, true);
		
		$data = array('title' => 'FAT Application | Log in');
		$this->load->view('default/header', $data);
		
		
		$associationsData = $this->user->get_associations();
		$data = array('associationsData' => $associationsData, 'navigation' => $navigation);
		$this->load->view('default/user/associations', $data);
		
		$this->load->view('default/footer');
	}	
	
	public function edit_profile()
	{
		$this->load->model('Users_model', 'user');
      	if( !$this->user->loggedin() )	redirect('/login', 'refresh');

		$firstname = $this->input->post('firstname');
		if(!$firstname){
		
			$is_admin = $this->user->is_admin();
			$data = array('navigation' => 1, 'is_admin' => $is_admin);
			$navigation = $this->load->view('default/user/navigation', $data, true);
			
			$data = array('title' => 'FAT Application | Log in');
			$this->load->view('default/header', $data);
		
		
			$userData = $this->user->get();
			$data = array('userData' => $userData, 'navigation' => $navigation);
			$this->load->view('default/user/edit_profile', $data);
			
			$this->load->view('default/footer');
		
		
		}else{
			$lastname = 			$this->input->post('lastname');
			$email = 				$this->input->post('email');
			
			$userData = $this->user->get();
			
			$data = array( 'firstname' => $firstname, 'lastname' => $lastname);
			$this->user->update($userData["id"], $data);
			
			echo json_encode(array('success'=>true, 'msg'=>'Profile updated successfully!'));
		}

	}
	
	public function add_association()
	{
		$this->load->model('Users_model', 'user');
      	if( !$this->user->loggedin() )	redirect('/login', 'refresh');

		$associationname = $this->input->post('raison_social');
		if(!$associationname){
			$is_admin = $this->user->is_admin();
			$data = array('navigation' => 11, 'is_admin' => $is_admin);
			$navigation = $this->load->view('default/user/navigation', $data, true);
			
			$data = array('title' => 'FAT Application | Log in');
			$this->load->view('default/header', $data);
		
		
			$userData = $this->user->get();
			$data = array('userData' => $userData, 'navigation' => $navigation);
			$this->load->view('default/user/add_association', $data);
			
			$this->load->view('default/footer');

		}else{
			$email = 			$this->input->post('mail');
			$fondateur = 				$this->input->post('fondateur');
			$date_fondation = 			$this->input->post('date_fondation');
			$contact = 			$this->input->post('contact');
			$telephone = 			$this->input->post('telephone');
			$adresse = 			$this->input->post('adresse');
			$description = $this->input->post('description');
			$location = $this->input->post('location');
			$location = explode(",", $location);
			
			$data = array( "raison_social" => $associationname, "mail" => $email,
							"fondateur" => $fondateur, "date_fondation" => $date_fondation,
							"contact" => $contact, "telephone" => $telephone,
							"adresse" => $adresse, "description" => $description,
							"location_lat" => $location[0], "location_lng" => $location[1]);
			
			$this->load->model('Associations_model', 'association');
			$associationID = $this->association->insert($data);
			
			$userData = $this->user->get();
			$userID = $userData["id"];
			$this->association->add_user($associationID, $userID, "admin");
			
			echo json_encode(array('success'=>true, 'msg'=>'Association added successfully!'));
		}

	}
	
	public function edit_association($id = 0)
	{
		if($id == 0 )
			$id = $this->input->post('id');
		
		$this->load->model('Users_model', 'user');
      	if( !$this->user->loggedin() ) {redirect('/login', 'refresh'); return;}
      	if( !$this->user->is_association_admin($id)) { echo json_encode(array('success'=>false, 'msg'=>'You are not admin of this association!')); return; }

		$associationname = $this->input->post('raison_social');
		if(!$associationname){
			$is_admin = $this->user->is_admin();
			$data = array('navigation' => -1, 'is_admin' => $is_admin);
			$navigation = $this->load->view('default/user/navigation', $data, true);
			
			$data = array('title' => 'FAT Application | Log in');
			$this->load->view('default/header', $data);
		
			$this->load->model('Associations_model', 'association');
			$associationData = $this->association->get_id($id);
			$associationData["location"] =$associationData["location_lat"].",".$associationData["location_lng"];
			
			$data = array('associationData' => $associationData, 'navigation' => $navigation);
			$this->load->view('default/user/edit_association', $data);
			
			$this->load->view('default/footer');

		}else{
			$email = 			$this->input->post('mail');
			$fondateur = 				$this->input->post('fondateur');
			$date_fondation = 			$this->input->post('date_fondation');
			$contact = 			$this->input->post('contact');
			$telephone = 			$this->input->post('telephone');
			$adresse = 			$this->input->post('adresse');
			$description = $this->input->post('description');
			$id = $this->input->post('id');
			$location = $this->input->post('location');
			$location = explode(",", $location);
			
			$data = array( "raison_social" => $associationname, "mail" => $email,
							"fondateur" => $fondateur, "date_fondation" => $date_fondation,
							"contact" => $contact, "telephone" => $telephone,
							"adresse" => $adresse, "description" => $description,
							"location_lat" => $location[0], "location_lng" => $location[1]);
			
			$this->load->model('Associations_model', 'association');
			$this->association->update($id, $data);
			
			echo json_encode(array('success'=>true, 'msg'=>'Association updated successfully!'));
		}

	}	
}

/* End of file welcome.php */
/* Location: ./application/controllers/users.php */